﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EasySamba.Controllers
{
    /// <summary>
    /// 配置节点（一个共享）
    /// </summary>
    public class ConfigNode
    {
        #region 属性
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 目录
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 只读
        /// </summary>
        public bool ReadOnly { get; set; }

        /// <summary>
        /// 用户
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// 任何人都可以访问
        /// </summary>
        public bool EveryOne { get; set; }

        /// <summary>
        /// 其他配置内容
        /// </summary>
        public Dictionary<string, string> OtherContent { get; set; }
        #endregion

        #region 构造函数
        public ConfigNode()
        {
            OtherContent = new Dictionary<string, string>();
        }
        #endregion

        #region 方法
        public void FromTextLines(IEnumerable<string> textLines)
        {
            //清空现有数据
            OtherContent.Clear();

            //检验数据有效性及获取共享节点名称
            var firstLine = textLines.FirstOrDefault();
            if (!string.IsNullOrWhiteSpace(firstLine) && firstLine.StartsWith('[') && firstLine.EndsWith(']'))
            {
                this.Name = firstLine.Substring(1, firstLine.Length - 2);

                //提取数据为字典
                var dataDict = new Dictionary<string, string>();
                foreach (var textLine in textLines.Skip(1))
                {
                    var keyValue = Comn.SplitKeyValue(textLine);
                    if (keyValue.Item1)
                    {
                        dataDict.Add(keyValue.Item2, keyValue.Item3);
                    }
                }

                //必要的配置
                if (dataDict.ContainsKey("available"))
                {
                    this.IsEnable = dataDict["available"] == "yes";
                    dataDict.Remove("available");
                }
                if (dataDict.ContainsKey("path"))
                {
                    this.Path = dataDict["path"];
                    dataDict.Remove("path");
                }
                if (dataDict.ContainsKey("read only"))
                {
                    this.ReadOnly = dataDict["read only"] == "yes";
                    dataDict.Remove("read only");
                }
                if (dataDict.ContainsKey("valid users"))
                {
                    this.User = dataDict["valid users"];
                    dataDict.Remove("valid users");
                }
                if (dataDict.ContainsKey("public"))
                {
                    this.EveryOne = dataDict["public"] == "yes";
                    dataDict.Remove("public");
                }

                //其他配置
                foreach (var item in dataDict)
                {
                    OtherContent.Add(item.Key, item.Value);
                }
            }
        }

        public IEnumerable<string> ToTextLines()
        {
            var result = new List<string>();
            result.Add($"[{this.Name}]");
            result.Add(Comn.ToLine("available", this.IsEnable ? "yes" : "no"));
            result.Add(Comn.ToLine("path", this.Path));
            result.Add(Comn.ToLine("read only", this.ReadOnly ? "yes" : "no"));
            result.Add(Comn.ToLine("valid users", this.User));
            result.Add(Comn.ToLine("public", this.EveryOne ? "yes" : "no"));
            if (!this.OtherContent.ContainsKey("comment"))
            {
                this.OtherContent.Add("comment", this.Name);
            }
            foreach (var item in this.OtherContent)
            {
                result.Add(Comn.ToLine(item.Key, item.Value));
            }

            return result;
        } 
        #endregion
    }
}
